package com.lzx.adx.report.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzx.adx.report.dao.TbCpaListDao;
import com.lzx.adx.report.dto.TbCpaListDTO;
import com.lzx.adx.report.entity.TbCpaListEntity;
import com.lzx.adx.report.service.TbCpaListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2025-02-23
 */
@Service
public class TbCpaListServiceImpl extends CrudServiceImpl<TbCpaListDao, TbCpaListEntity, TbCpaListDTO> implements TbCpaListService {

    @Override
    public QueryWrapper<TbCpaListEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<TbCpaListEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public TbCpaListEntity getDetail(String date, String type, String pid) {
        LambdaQueryWrapper<TbCpaListEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbCpaListEntity::getDay,date);
        wrapper.eq(TbCpaListEntity::getType,type);
        wrapper.eq(TbCpaListEntity::getPid,pid);
        List<TbCpaListEntity> tbCpaListEntities = this.baseDao.selectList(wrapper);

        if(CollectionUtil.isNotEmpty(tbCpaListEntities)){
            return tbCpaListEntities.get(0);
        }
        return null;
    }

    @Override
    public void handTbCappData(TbCpaListDTO tbCpaListDTO) {
        TbCpaListEntity detail = getDetail(tbCpaListDTO.getDay(), tbCpaListDTO.getType(), tbCpaListDTO.getPid());
        if(Objects.nonNull(detail)){
            tbCpaListDTO.setId(detail.getId());
            update(tbCpaListDTO);
        }else{
            save(tbCpaListDTO);
        }

    }
}